package com.johannting.demo.job.jdbc;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;
import java.util.List;

public class JDBCQuartz {
    public static void main(String[] args) throws SchedulerException,ParseException{
            startSchedule();
        }

    public static void startSchedule(){

        try {
            // 1、创建一个JobDetail实例，指定Quartz
            JobDetail jobDetail= JobBuilder.newJob(JDBCJob.class)
                    .withIdentity("job1_1","jGroup1")
                    .build();
                //触发器类型
            CronScheduleBuilder builder=CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
            // 2、创建Trigger
            Trigger trigger= TriggerBuilder.newTrigger()
                    .withIdentity("trigger1","tGroup1").startNow()
                    .withSchedule(builder)
                    .build();
            // 3、创建Scheduler
            Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();
            scheduler.scheduleJob(jobDetail,trigger);
            // 4、开启调度
            scheduler.start();

            // 10min停
            try {
                Thread.sleep(600000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //关闭调度器
            scheduler.shutdown();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void resumeJob(){

        try {
            SchedulerFactory schedulerFactory=new StdSchedulerFactory();
            Scheduler scheduler=schedulerFactory.getScheduler();
            JobKey jobKey=new JobKey("job1_1","jGroup1");
            List<? extends Trigger> triggers=scheduler.getTriggersOfJob(jobKey);
            if(triggers.size()>0){
                for(Trigger tg:triggers){
                    if((tg instanceof CronTrigger) || (tg instanceof SimpleTrigger)){
                        scheduler.resumeJob(jobKey);
                    }
                }
                scheduler.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
